package com.itheima.test;

import com.itheima.demo.pojo.Evection;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class ActivitiGatewayExclusive {
    /**
     * 流程部署
     */
    @Test
    public void testDeployment(){
        //1、创建ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2、获取RepositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();

        //3、使用RepositoryService流程部署：把bpmn和png部署到数据库中，并给流程定义一个名字
        Deployment deployment = repositoryService.createDeployment()
                .name("evection-exclusive")
                .addClasspathResource("bpmn/evection-exclusive.bpmn20.xml")
                .deploy();


        //4、输出部署的信息
        System.out.println("流程部署id="+deployment.getId());
        System.out.println("流程部署名字="+deployment.getName());
    }

    /**
     * 启动流程时设置变量
     */
    @Test
    public void testStartProcess(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        Map<String,Object> variables = new HashMap<>();
        Evection evection = new Evection();
        evection.setNum(2d);
        variables.put("evection",evection);
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("evection-exclusive",variables);
        System.out.println("processInstance.getId() = " + processInstance.getId());//
    }

    @Test
    public void testCompleteTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        Task task = taskService.createTaskQuery()
                .processDefinitionKey("evection-exclusive")
//                .taskAssignee("张三")
                .taskAssignee("部门经理")
                .singleResult();
        if (task != null) {
            taskService.complete(task.getId());
        }
    }
}
